﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BaoXiu : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sql = "select distinct (楼号),ID from 公寓信息";
        DataSet ds = new DataSet();
        ds = new Class1().hsggetdata(sql);
        this.DropDownList1.DataSource = ds.Tables[0];
        this.DropDownList1.DataTextField = "楼号";
        this.DropDownList1.DataValueField = "ID";
        this.DropDownList1.DataBind();
        dr2(); dr3();
        this.DropDownList1.Items.Insert(0, new ListItem("--请选择--", "0"));
            if (Request["ID"] != null)
            {
                InitData();
            }
        } 
        
       
       
    }

    protected void InitData()
    {

        int id = int.Parse(Request["ID"].ToString());

        try
        {


            DataSet ds = new DataSet();
            ds = new Class1().hsggetdata("select * from 保修信息 where id=" + id);
            if (ds.Tables[0].Rows.Count > 0)
            {
                this.DropDownList1.Text = ds.Tables[0].Rows[0]["楼号"].ToString();
                this.DropDownList2.Text = ds.Tables[0].Rows[0]["所在学院"].ToString();
                this.DropDownList3.Text = ds.Tables[0].Rows[0]["宿舍号"].ToString();


                this.TextBox1.Text = ds.Tables[0].Rows[0]["保修物品名称"].ToString();
                this.TextBox2.Text = ds.Tables[0].Rows[0]["保修人"].ToString();
                this.TextBox3.Text = ds.Tables[0].Rows[0]["修理结果"].ToString();
               
               
            }


        }
        catch
        {



        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql;
        


        if (Request["ID"] != null)
        {
            int id = int.Parse(Request["ID"].ToString());
            sql = "update 保修信息 set 楼号='" + this.DropDownList1.SelectedItem.Text + "',所在学院='" + this.DropDownList2.SelectedItem.Text + "',宿舍号='" + this.DropDownList3.SelectedItem.Text + "',保修物品名称='" + TextBox1.Text + "',保修人='" + TextBox2.Text + "',修理结果='" + TextBox3.Text + "' where id=" + id;
            int result;
            result = new Class1().hsgexucute(sql);
            if (result == 1)
            {
                Response.Write("<script>javascript:alert('操作成功');</script>");
            }
            else
            {
                Response.Write("<script>javascript:alert('系统错误，您没有设置好数据库的连接！或您输入了一些非法字符！');</script>");
            }

        }
        else
        {
            sql = "insert into 保修信息(楼号,所在学院,宿舍号,保修物品名称,保修人,修理结果) values('" + this.DropDownList1.SelectedItem.Text + "','" + this.DropDownList2.SelectedItem.Text + "','" + this.DropDownList3.SelectedItem.Text  + "','" + this.TextBox1.Text + "','" + this.TextBox2.Text + "','" + this.TextBox3.Text + "')";

            int result;
            result = new Class1().hsgexucute(sql);
            if (result == 1)
            {
                Response.Write("<script>javascript:alert('添加成功');</script>");
            }
            else
            {
                Response.Write("<script>javascript:alert('系统错误，您没有设置好数据库的连接！');</script>");
            }
        }
    }









    //楼号
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        dr2();

    }
    //所在学院
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        dr3();
    }
    public void dr2()
    {
        string sql = "select distinct (所在学院),ID from 公寓信息 where 楼号='" + this.DropDownList1.SelectedItem.Text + "'";
        DataSet ds = new DataSet();
        ds = new Class1().hsggetdata(sql);
        this.DropDownList2.DataSource = ds.Tables[0];
        this.DropDownList2.DataTextField = "所在学院";
        this.DropDownList2.DataValueField = "ID";
        this.DropDownList2.DataBind();
        this.DropDownList2.Items.Insert(0, new ListItem("--请选择--", "0"));

    }

    public void dr3()
    {
        string sql = "select * from 公寓信息 where 所在学院='" + this.DropDownList2.SelectedItem.Text + "'";
        DataSet ds = new DataSet();
        ds = new Class1().hsggetdata(sql);
        this.DropDownList3.DataSource = ds.Tables[0];
        this.DropDownList3.DataTextField = "房间号";
        this.DropDownList3.DataValueField = "ID";
        this.DropDownList3.DataBind();
       this.DropDownList3.Items.Insert(0, new ListItem("--请选择--", "0"));
    }
    protected void DropDownList2_TextChanged(object sender, EventArgs e)
    {
        dr3();

    }
    protected void DropDownList1_TextChanged(object sender, EventArgs e)
    {
        dr2();
    }
}
